using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using NUnit.Framework;

using Amazon.SimpleDB;

using NSimpleDB.Service.Contract;
using NSimpleDB.Service.Amazon;
using NSimpleDB.Service.Amazon.Dashboard;

namespace test.Service.Amazon.online
{
    [TestFixture]
    public class testDashboard
    {
        [Test]
        public void testMetaData()
        {
            SimpleDBDashboard dashboard = new SimpleDBDashboard();

            ISimpleDBResponseMetaData rmd;
            rmd = dashboard.GetResponseMetaData();
            Assert.IsNull(rmd);
            //Assert.AreEqual("", rmd.RequestId);
            //Assert.AreEqual("", rmd.BoxUsage);

            dashboard.SetResponseMetaData("r", "b");
            rmd = dashboard.GetResponseMetaData();
            Assert.IsNotNull(rmd);
            Assert.AreEqual("r", rmd.RequestId);
            Assert.AreEqual("b", rmd.BoxUsage);

            dashboard.SetResponseMetaData("r2", "b2");
            rmd = dashboard.GetResponseMetaData();
            Assert.IsNotNull(rmd);
            Assert.AreEqual("r2", rmd.RequestId);
            Assert.AreEqual("b2", rmd.BoxUsage);

            // set meta data in backgroun thread
            // they should not be visible in foreground thread
            AutoResetEvent are = new AutoResetEvent(false);
            ThreadPool.QueueUserWorkItem(delegate 
                {
                    dashboard.SetResponseMetaData("rb", "bb");
                    are.Set();
                });
            are.WaitOne();
            rmd = dashboard.GetResponseMetaData();
            Assert.AreEqual("r2", rmd.RequestId);
            Assert.AreEqual("b2", rmd.BoxUsage);
        }


        [Test]
        public void testException()
        {
            SimpleDBDashboard dashboard = new SimpleDBDashboard();

            ISimpleDBExceptionData ed;
            ed = dashboard.GetExceptionData();
            Assert.IsNull(ed);
            //Assert.AreEqual("", ed.ErrorCode);
            //Assert.AreEqual("", ed.RequestId);
            //Assert.AreEqual("", ed.BoxUsage);

            AmazonSimpleDBException ex;
            ex = new AmazonSimpleDBException("err", System.Net.HttpStatusCode.BadRequest, "errcode", "errtype", "boxusage", "requestid", "xml");
            dashboard.SetExceptionData(ex);
            ed = dashboard.GetExceptionData();
            Assert.AreEqual("err", ed.Message);
            Assert.AreEqual("errcode", ed.ErrorCode);
            Assert.AreEqual("requestid", ed.RequestId);

            ex = new AmazonSimpleDBException("err2", System.Net.HttpStatusCode.BadRequest, "errcode2", "errtype", "boxusage", "requestid", "xml");
            dashboard.SetExceptionData(ex);
            ed = dashboard.GetExceptionData();
            Assert.AreEqual("err2", ed.Message);
            Assert.AreEqual("errcode2", ed.ErrorCode);

            ISimpleDBResponseMetaData rmd;
            dashboard.SetResponseMetaData("r", "b");
            rmd = dashboard.GetResponseMetaData();
            Assert.IsNotNull(rmd);
            Assert.IsNull(dashboard.GetExceptionData());
            Assert.AreEqual("r", rmd.RequestId);
            Assert.AreEqual("b", rmd.BoxUsage);
        }
    }
}
